package com.example.orderassembly.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.UpdateChainWrapper;
import com.example.orderassembly.service.IAfterSaleService;
import com.example.orderassembly.service.impl.AfterSaleServiceImpl;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import com.woniu.cloud.common.exception.TicketException;
import com.woniu.cloud.common.utils.JWTHelper;
import com.woniu.cloud.common.utils.LoginHelper;
import com.woniuxy.api.AfterSaleApi;
import com.woniu.cloud.common.dto.Result;
import com.woniuxy.entity.AfterSale;
import com.woniuxy.form.GetAfterSaleByPlatform;
import com.woniuxy.form.InsertAfterSale;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBucket;
import org.redisson.api.RKeys;
import org.redisson.api.RSet;
import org.redisson.api.RedissonClient;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 * 售后表 前端控制器
 * </p>
 *
 * @author yang
 * @since 2021-09-21
 */
@RestController
@Slf4j
public class AfterSaleController implements AfterSaleApi {
    @Resource
    AfterSaleServiceImpl afterSaleService;
    @Resource
    private RedissonClient redissonClient;

    @GetMapping("/login")
    public  void login(){
        RSet<String> rsset = redissonClient.getSet("rsset");

        rsset.add("a");
        rsset.add("b");
        rsset.add("c");
        rsset.add("d");
        RBucket<Object> bucket = redissonClient.getBucket("cache:user211:1");
        RKeys keys = redissonClient.getKeys();
        Iterator<String> iterator = keys.getKeys().iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        System.out.println(bucket.get());
    }


    /**
     * 已测
     * @param afterSaleId  售后id
     * @param status  状态码  1.未完成   2.已完成   3.不能售后
     * @return
     */
    @PutMapping("/updateAfterSaleById")
    @Override
    @GlobalTransactional
    public Result updateAfterSaleById(Integer afterSaleId, Integer status) {
        log.info("进入到平台修改修改商品订单的售后状态，参数 afterSaleId={},status={}",afterSaleId,status);
        if(null==afterSaleId||null==status){
            log.warn("参数异常");
            throw new TicketException("参数异常");
        }
        Boolean bool = afterSaleService.updateAfterSaleById(afterSaleId,status);

        return bool?Result.success("修改成功",null):Result.fail("修改失败",null);
    }
    /**
     *已测
     * @param pageSize 总页数
     * @param indexPage　页数
     * @return　Iｐａｇｅ
     *
     * 用户查看商品订单的售后订单详情
     */
    @GetMapping("/getAfterSaleByUserId")
    @Override
    @GlobalTransactional
    public Result getAfterSaleByUserId(Integer indexPage,Integer pageSize,Integer userId) {
        log.info("进入到用户查看商品订单的售后订单详情 indexPage = {}  pageSize= {}",indexPage,pageSize);
        if(null==pageSize||indexPage==null){
            log.warn("参数错误");
//            return Result.fail("参数错误",null);
            throw new TicketException("参数异常");
        }

//        int userId = LoginHelper.getId("userId");
        IPage iPage = afterSaleService.getAfterSaleByUserId(userId,indexPage,pageSize);
        return iPage.getRecords().size()==0?Result.fail("查询失败",iPage):Result.success("成功",iPage);
    }

    /**
     *已测
     * @param getAfterSaleByPlatform
     * @return
     */
    @GetMapping("/getAfterSaleByPlatform")
    @Override
    @GlobalTransactional
    public Result getAfterSaleByPlatform(GetAfterSaleByPlatform getAfterSaleByPlatform) {
        log.info("平台查看售后订单详情 getAfterSaleByPlatform={}",getAfterSaleByPlatform);
        if(getAfterSaleByPlatform==null){
            log.info("参数错误");
//            return Result.fail("参数错误",null);
            throw new TicketException("参数异常");
        }else if(getAfterSaleByPlatform.getIndexPage()==null||getAfterSaleByPlatform.getPageSize()==null||getAfterSaleByPlatform.getIndexPage()==0||getAfterSaleByPlatform.getPageSize()==0){
            log.info("参数错误");
            return Result.fail("参数错误",null);
        }
        IPage iPage = afterSaleService.getAfterSaleByPlatform(getAfterSaleByPlatform);

        return iPage.getRecords().size()==0?Result.fail("查找失败",null):Result.success("查找成功",iPage);
    }

    /**
     *  已测
     * @param insertAfterSale
     * @return
     */
    @PostMapping("/insertAfterSale")
    @Override
    @GlobalTransactional
    public Result insertAfterSale(InsertAfterSale insertAfterSale) {
        log.info("进入到用户申请售后，生成售后订单方法中");
        if(insertAfterSale==null){
            log.info("参数错误");
//            return Result.fail("参数错误",null);
            throw new TicketException("参数异常");
        }
        Boolean bool = afterSaleService.insertAfterSale(insertAfterSale);

        return bool?Result.success("申请售后状态已改变",null):Result.fail("失败",null);
    }


    /**
     *已测
     * @param getAfterSaleByPlatform 页数
     * @return
     */
    @GetMapping("/getAfterSaleByStore")
    @Override
    @GlobalTransactional
    public Result getAfterSaleByStore(GetAfterSaleByPlatform getAfterSaleByPlatform) {
        log.info("进入到店铺获取当前店家的售后订单详情");
        //todo 通过 jwt 获取店家id
        Integer storeId = 1;
        if(null==getAfterSaleByPlatform){
            log.info("参数错误");
//            return Result.fail("参数错误",null);
            throw  new TicketException("参数异常");
        }

        IPage iPage = afterSaleService.getAfterSaleByStore(getAfterSaleByPlatform);

        return iPage.getRecords().size()==0?Result.fail("查询失败",iPage):Result.success("成功",iPage);
    }

    /**
     *已测
     * @param afterSaleId   售后Id
     * @return
     */
    @Override
    @GlobalTransactional
    public Result getAfterSaleById(Integer afterSaleId) {
        log.info("获取某一个售后订单信息");
        if (afterSaleId==null){
            log.info("参数错误");
//            return Result.fail("参数错误",null);
            throw new TicketException("参数异常");
        }
        AfterSale iPage = afterSaleService.getAfterSaleById(afterSaleId);

        return iPage==null?Result.fail("查询失败",iPage):Result.success("成功",iPage);
    }
}

